import Vue from 'vue'

export const JeecgListMixin = {
  data() {
    return {
      //token header
      tokenHeader: {
        'X-Access-Token': "",
      },
      /* 查询条件-请不要在queryParam中声明非字符串值的属性 */
      queryParam: {},
      /* 数据源 */
      dataSource: [],
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + "-" + range[1] + " 共" + total + "条"
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      /* 排序参数 */
      isorter: {
        column: 'createTime',
        order: 'desc',
      },
      /* 筛选参数 */
      filters: {},
      /* table加载状态 */
      loading: false,
      /* table选中keys*/
      selectedRowKeys: [],
      /* table选中records*/
      selectionRows: [],
      /* 查询折叠 */
      toggleSearchStatus: false,
      /* 高级查询条件生效状态 */
      superQueryFlag: false,
      /* 高级查询条件 */
      superQueryParams: '',
      /** 高级查询拼接方式 */
      superQueryMatchType: 'and',
    }
  },
  created() {
   
  },
  methods: {
 
    // 表格插槽修改
    handleEdit: function (record) {
      // this.$refs.modalForm.edit(this.selectionRows[0]);
      // this.$refs.modalForm.title = "编辑";
      // this.$refs.modalForm.disableSubmit = false
      console.log(record)
      this.$refs.modalForm.edit(record);
      this.$refs.modalForm.title = "编辑";
      this.$refs.modalForm.disableSubmit = false;

    },
    // 按钮修改
    handleEdite: function (record) {
      // this.$refs.modalForm.edit(this.selectionRows[0]);
      // this.$refs.modalForm.title = "编辑";
      // this.$refs.modalForm.disableSubmit = false
      console.log(record)
      if (record.length == 1) {
        this.$refs.modalForm.edit(record);
        this.$refs.modalForm.title = "编辑";
        this.$refs.modalForm.disableSubmit = false;
      } else {
        this.$message.warning('请选择一条记录')
      }
    },

    handleAdd (record) {
    console.log(record)
    this.$refs.modalForm.add();
    },

  
   
    /* 导出 */
    handleExportXls2() {
      let paramsStr = encodeURI(JSON.stringify(this.getQueryParams()));
      let url = `${window._CONFIG['domianURL']}/${this.url.exportXlsUrl}?paramsStr=${paramsStr}`;
      window.location.href = url;
    },
    // handleExportXls(fileName) {
    //   if (!fileName || typeof fileName != "string") {
    //     fileName = "导出文件"
    //   }
    //   let param = {
    //     ...this.queryParam
    //   };
    //   if (this.selectedRowKeys && this.selectedRowKeys.length > 0) {
    //     param['selections'] = this.selectedRowKeys.join(",")
    //   }
    //   console.log("导出参数", param)
    //   downFile(this.url.exportXlsUrl, param).then((data) => {
    //     if (!data) {
    //       this.$message.warning("文件下载失败")
    //       return
    //     }
    //     if (typeof window.navigator.msSaveBlob !== 'undefined') {
    //       window.navigator.msSaveBlob(new Blob([data], {
    //         type: 'application/vnd.ms-excel'
    //       }), fileName + '.xls')
    //     } else {
    //       let url = window.URL.createObjectURL(new Blob([data], {
    //         type: 'application/vnd.ms-excel'
    //       }))
    //       let link = document.createElement('a')
    //       link.style.display = 'none'
    //       link.href = url
    //       link.setAttribute('download', fileName + '.xls')
    //       document.body.appendChild(link)
    //       link.click()
    //       document.body.removeChild(link); //下载完成移除元素
    //       window.URL.revokeObjectURL(url); //释放掉blob对象
    //     }
    //   })
    // },
    handleExportXls(fileName) {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！');
        return;
      } else {
        this.toExcel(fileName, this.selectionRows, this.columns)
      }
    },
    /* 导入 */
    handleImportExcel(info) {
      if (info.file.status !== 'uploading') {
        console.log(info.file, info.fileList);
      }
      if (info.file.status === 'done') {
        if (info.file.response.success) {
          this.$message.success(`${info.file.name} 文件上传成功`);
          if (info.file.response.code === 201) {
            let {
              message,
              result: {
                msg,
                fileUrl,
                fileName
              }
            } = info.file.response
            let href = window._CONFIG['domianURL'] + fileUrl
            // this.$warning({
            //     title: message,
            //     content: ( < div > < span > {
            //         msg
            //       } < /span><br/ > < span > 具体详情请 < a href = {
            //         href
            //       }
            //       target = "_blank"
            //       download = {
            //         fileName
            //       } > 点击下载 < /a> </span > < /div>)
            //     })
            // }
            // else {
            //   this.$message.success(info.file.response.message || `${info.file.name} 文件上传成功`)
            // }
            this.loadData()
          } else {
            this.$message.error(`${info.file.name} ${info.file.response.message}.`);
          }
        } else if (info.file.status === 'error') {
          this.$message.error(`文件上传失败: ${info.file.msg} `);
        }
      }
    },
    /* 图片预览 */
    getImgView(text) {
      if (text && text.indexOf(",") > 0) {
        text = text.substring(0, text.indexOf(","))
      }
      return getFileAccessHttpUrl(text)
    },
    /* 文件下载 */
    uploadFile(text) {
      if (!text) {
        this.$message.warning("未知的文件")
        return;
      }
      if (text.indexOf(",") > 0) {
        text = text.substring(0, text.indexOf(","))
      }
      let url = getFileAccessHttpUrl(text)
      window.open(url);
    },
    toExcel(fileName, data, columns) {
      const option = {};
      option.fileName = fileName;
      option.datas = [{
        sheetData: data.map((item, index) => {
          const result = {};
          columns.forEach((c) => {
            // if(c.dataIndex == 'no'){
            //     result[c.dataIndex] = index+1;
            // }else if(c.dataIndex == 'createdTime'){
            //     result[c.dataIndex] = c.dataIndex;
            // }else{
            result[c.dataIndex] = item[c.dataIndex];
            // }
          });
          return result;
        }),
        sheetName: fileName, // Excel文件名称
        sheetFilter: columns.map((item) => item.dataIndex),
        sheetHeader: columns.map((item) => item.title),
        columnWidths: columns.map(() => 10),
      }]
      const toExcel = new ExportJsonExcel(option);
      toExcel.saveExcel();
    }
  }

}